package com.iot.controller;

import com.iot.compont.GatewayInstance;
import com.iot.compont.GatewayServerEnum;
import com.iot.compont.ServerNetwork;
import com.iot.gateway.GatewayEntity;
import com.iot.http.HttpServerNetwork;
import com.iot.mqtt.MqttServerNetwork;
import com.iot.socket.SoketServerNetwork;
import com.iot.tcp.TcpServerNetwork;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Administrator
 */
@RestController
public class GatewayController {

    @Resource
    GatewayInstance gatewayInstance;

    @GetMapping
    public void save() {


        ServerNetwork serverNetworkByName = GatewayServerEnum.HTTP.createServerNetworkByName();
        HttpServerNetwork httpServerNetwork = (HttpServerNetwork) serverNetworkByName;


        GatewayEntity gatewayEntity = new GatewayEntity();
        gatewayEntity.setId("11111");
        gatewayEntity.setHost("0.0.0.0");
        gatewayEntity.setProd(18801);

        ServerNetwork map = gatewayInstance.getMap(gatewayEntity.getId());
        if (map != null) {
            map.cloneServer();
        }

        ServerNetwork server = httpServerNetwork.createServer(gatewayEntity);
        gatewayInstance.putMap(gatewayEntity.getId(), server);


        ServerNetwork tcpServerNetworkByName = GatewayServerEnum.TCP.createServerNetworkByName();
        TcpServerNetwork tcpServerNetwork = (TcpServerNetwork) tcpServerNetworkByName;
        GatewayEntity tcpGateway = new GatewayEntity();
        tcpGateway.setId("33333");
        tcpGateway.setHost("10.8.20.238");
        tcpGateway.setProd(18802);
        ServerNetwork tcp = gatewayInstance.getMap(tcpGateway.getId());
        if (tcp != null) {
            tcp.cloneServer();
        }
        ServerNetwork tcpserver = tcpServerNetwork.createServer(tcpGateway);
        gatewayInstance.putMap(tcpGateway.getId(), tcpserver);


        ServerNetwork socketServer = GatewayServerEnum.SOCKET.createServerNetworkByName();
        SoketServerNetwork soketServerNetwork = (SoketServerNetwork) socketServer;
        GatewayEntity socketGateway = new GatewayEntity();
        socketGateway.setId("44444");
        socketGateway.setHost("10.8.20.238");
        socketGateway.setProd(18803);
        ServerNetwork socket = gatewayInstance.getMap(socketGateway.getId());
        if (socket != null) {
            socket.cloneServer();
        }
        ServerNetwork serverNetwork = soketServerNetwork.createServer(socketGateway);
        gatewayInstance.putMap(socketGateway.getId(), serverNetwork);


        ServerNetwork mqttServerNetworkByName = GatewayServerEnum.MQTT.createServerNetworkByName();
        MqttServerNetwork mqttServerNetwork = (MqttServerNetwork) mqttServerNetworkByName;
        GatewayEntity mqttGateway = new GatewayEntity();
        mqttGateway.setId("55555");
        mqttGateway.setHost("10.8.20.238");
        mqttGateway.setProd(18804);
        ServerNetwork mqtt = gatewayInstance.getMap(socketGateway.getId());
        if (mqtt != null) {
            mqtt.cloneServer();
        }
        ServerNetwork mqttServer = mqttServerNetwork.createServer(mqttGateway);
        gatewayInstance.putMap(mqttGateway.getId(), mqttServer);
    }

    @GetMapping("/device")
    public void getDevice() {
        gatewayInstance.getAll().entrySet().forEach(System.out::println);
    }
}
